﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace KTX.Data
{
    class ClsKetNoi
    {
        SqlConnection conn = new SqlConnection();        
        string ketnoi = ConfigurationManager.ConnectionStrings["project"].ConnectionString;        
        //string ketnoi = "Data Source=.\\SQLEXPRESS;Initial Catalog=DB_KTX;Persist Security Info=True;User ID=sa;Password=0918606772";
        public ClsKetNoi()        
        {           
                conn.ConnectionString = ketnoi;
                if (conn.State == ConnectionState.Closed)
                    conn.Open();             
        }
        
        public void Close()
        {
            conn.Dispose();
        }

        public DataTable LayDuLieu(string sql)
        {
            SqlCommand command = new SqlCommand(sql,conn);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            command.Dispose();
            adapter.Dispose();
            dt.Dispose();
            return dt;
        }

        public DataTable LayDuLieu(string sql, string[] name, object[] value, int Nparameter)
        {
            SqlCommand command = new SqlCommand(sql, conn);
            for (int i = 0; i < Nparameter; i++)
            {
                command.Parameters.AddWithValue(name[i], value[i]);
            }
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            command.Dispose();
            adapter.Dispose();
            dt.Dispose();
            return dt;
        }

        public int Update(string sql)
        {
            SqlCommand command = new SqlCommand(sql, conn);
            command.Dispose();
            int kq = command.ExecuteNonQuery();
            return kq;
        }

        public int Update(string sql, string[] name, object[] value, int Nparameter)
        {
            SqlCommand command = new SqlCommand(sql, conn);
            for (int i = 0; i < Nparameter; i++)
            {
                command.Parameters.AddWithValue(name[i], value[i]);
            }
            command.Dispose();
            int kq = command.ExecuteNonQuery();
            return kq;
        }
    }
}
